package com.rqbl.controller;


import com.rqbl.entity.User;
import com.rqbl.myutils.PageBean;
import com.rqbl.myutils.TableJson;
import com.rqbl.service.SubscribeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bruce
 * @since 2024-07-19
 */
@Controller
@RequestMapping("/subscribe")
public class SubscribeController {

    @Autowired
    private SubscribeService subscribeService;

    @RequestMapping("/getSubscribeByBid")
    @ResponseBody
    public TableJson getSubscribeByBid(Integer bid, Integer page, Integer limit){
        PageBean pageBean = new PageBean(page,limit,null,null);
        TableJson tableJson = new TableJson(0,"获取成功",null,null);
        tableJson.setData(subscribeService.getSubscribeByBid(pageBean,bid));
        tableJson.setCount(pageBean.getTotalSize());
        return tableJson;
    }

    @RequestMapping("insertSubscribeInfo")
    @ResponseBody
    public String insertSubscribeInfo(Integer bid, HttpSession session){
        User user = (User) session.getAttribute("user");
        subscribeService.insertSubscribeInfo(user.getUid(),bid);
        return "ok";
    }

    @RequestMapping("deleteSubInfo")
    @ResponseBody
    public String deleteSubInfo(Integer bid,HttpSession session){
        User user = (User) session.getAttribute("user");
        subscribeService.deleteSubInfo(user.getUid(),bid);
        return "ok";
    }

    @RequestMapping("getSubscribeByUB")
    @ResponseBody
    public String getSubscribeByUB(Integer bid, HttpSession session){
        User user = (User) session.getAttribute("user");
        return subscribeService.getSubscribeByUB(user.getUid(),bid)>0?"true":"false";
    }

    @RequestMapping("deleteSubscribeByUB")
    @ResponseBody
    public String deleteSubscribeByUB(Integer bid, HttpSession session) {
        User user = (User) session.getAttribute("user");
        subscribeService.deleteSubInfo(user.getUid(), bid);
        return "ok";
    }

    @RequestMapping("getSubscribeByUid")
    @ResponseBody
    public TableJson getSubscribeByUid(HttpSession session, Integer page, Integer limit){
        User user = (User) session.getAttribute("user");
        PageBean pageBean = new PageBean(page,limit,null,null);
        TableJson tableJson = new TableJson(0,"获取成功",null,null);
        tableJson.setData(subscribeService.getSubscribeByUid(pageBean,user.getUid()));
        tableJson.setCount(pageBean.getTotalSize());
        return tableJson;
    }
}
